<!DOCTYPE html>
<body onload="runTest();">
<div>This tests the restrictions to entering full screen mode laid out in section 4.1 of the W3C 
<a href="http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html">Full Screen API</a></div>
<script src="full-screen-test.js"></script>
<script>
    var runTest = function() {
        consoleWrite('"The context object is not in a document."');
        var div = document.createElement('div');
        waitForEventOnce(document, 'webkitfullscreenerror', step2);
        runWithKeyDown(function(){div.webkitRequestFullscreen()});
    };

    var step2 = function() {
        consoleWrite('"The context object\'s node document, or an ancestor browsing context\'s document does not have the fullscreen enabled flag set."')
        var iframe = document.documentElement.appendChild(document.createElement('iframe'));
        var div = iframe.contentDocument.documentElement.appendChild(iframe.contentDocument.createElement('div'));

        waitForEventOnce(iframe.contentDocument, 'webkitfullscreenerror', step3);
        runWithKeyDown(function(){div.webkitRequestFullscreen()});
    };

    var step3 = function() {
        consoleWrite('"The context object\'s node document fullscreen element stack is not empty and its top element is not an ancestor of the context object."');
        var div = document.documentElement.appendChild(document.createElement('div'));
        var div2 = document.documentElement.appendChild(document.createElement('div'));
        waitForEventOnce(document, 'webkitfullscreenchange', function() {
            waitForEventOnce(document, 'webkitfullscreenerror', function() {
               waitForEventOnce(document, 'webkitfullscreenchange', step4);
               document.webkitExitFullscreen();
            });
            runWithKeyDown(function(){div2.webkitRequestFullscreen()});
        });
        runWithKeyDown(function(){div.webkitRequestFullscreen()});
    };

    var step4 = function() {
        consoleWrite('"A descendant browsing context\'s document has a non-empty fullscreen element stack."');
        var iframe = document.documentElement.appendChild(document.createElement('iframe'));
        iframe.setAttribute('allowfullscreen', 'true');
        var div = iframe.contentDocument.documentElement.appendChild(iframe.contentDocument.createElement('div'));
        var div2 = document.documentElement.appendChild(document.createElement('div'));
        waitForEventOnce(iframe.contentDocument, 'webkitfullscreenchange', function() {
            waitForEventOnce(document, 'webkitfullscreenerror', function(){
                waitForEventOnce(iframe.contentDocument, 'webkitfullscreenchange', step5);
                iframe.contentDocument.webkitExitFullscreen();
            });
            runWithKeyDown(function(){div2.webkitRequestFullscreen()});
        });
        runWithKeyDown(function(){div.webkitRequestFullscreen()});
    };

    var step5 = function() {
        consoleWrite('"This algorithm is not allowed to show a pop-up."');
        var div = document.documentElement.appendChild(document.createElement('div'));
        waitForEventOnce(document, 'webkitfullscreenerror', endTest);
        div.webkitRequestFullscreen();
    };

    if (typeof(testRunner) != 'undefined')
        testRunner.setPopupBlockingEnabled(true);
</script>
